package org.jeecg.modules.demo.mpwlwjc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.demo.mpwlwjc.entity.ZwglDpsblist;
import org.jeecg.modules.demo.mpwlwjc.mapper.ZwglDpsblistMapper;
import org.jeecg.modules.demo.mpwlwjc.service.IZwglDpsblistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @Description: 大棚设备列表
 * @Author: jeecg-boot
 * @Date: 2023-04-14
 * @Version: V1.0
 */
@Service
public class ZwglDpsblistServiceImpl extends ServiceImpl<ZwglDpsblistMapper, ZwglDpsblist> implements IZwglDpsblistService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ZwglDpsblistMapper zwglDpsblistMapper;

    @Override
    public String getToken() {
        final String CONTENT_TYPE_TEXT_JSON = "text/json";
        DefaultHttpClient client = new DefaultHttpClient(
                new PoolingClientConnectionManager());

        String url = "https://agro-iot.auto-control.com.cn/api/account/sign-in";
        String js = "{\"username\":\"hangtianhengxing\",\"password\":\"12345\"}";

        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

        StringEntity se = null;
        try {
            se = new StringEntity(js);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        se.setContentType(CONTENT_TYPE_TEXT_JSON);
        httpPost.setEntity(se);

        CloseableHttpResponse response2 = null;

        try {
            response2 = client.execute(httpPost);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        HttpEntity entity2 = null;
        entity2 = response2.getEntity();
        String s2 = null;
        try {
            s2 = EntityUtils.toString(entity2, "UTF-8");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(s2);
        JSONObject jsonObject = JSON.parseObject(s2);
        System.out.println(jsonObject);
        JSONObject jsonObject1 = jsonObject.getJSONObject("data");
        String token = jsonObject1.getString("token");
        System.out.println(token);
        redisUtil.set("mptoken", token, 7200);
        return null;
    }

    @Override
    public String getmplist() {
        //得到long类型当前时间
        long l = System.currentTimeMillis();
        //new日期对象
        Date date = new Date(l);
        //转换提日期输出格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = dateFormat.format(date);
        //调用的api的接口地址

        String apiPath = "https://agro-iot.auto-control.com.cn/api/device";
        BufferedReader in = null;
        StringBuffer result = null;
        String token = (String) redisUtil.get("mptoken");
        if (Objects.isNull(token)) {
            getToken();
        }

        try {
            URL url = new URL(apiPath);
            //打开和url之间的连接

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);


            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Authorization", token);
            connection.setRequestProperty("Charset", "utf-8");
            connection.connect();
            result = new StringBuffer();
            //读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            String result2 = result.toString(); //返回json字符串
            System.out.println(result2);
            //获取数据
            JSONObject jsonObject = JSON.parseObject(result2);
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            System.out.println(jsonArray.size());
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                ZwglDpsblist zwglDpsblist = new ZwglDpsblist();
                zwglDpsblist.setMc(jsonObject2.getString("name"));
                zwglDpsblist.setSbbh(jsonObject2.getString("code"));
                zwglDpsblist.setSbmc(jsonObject2.getString("deviceTypeName"));
                zwglDpsblist.setSblxbh(jsonObject2.getString("deviceTypeId"));
                zwglDpsblist.setZdmc(jsonObject2.getString("stationName"));
                zwglDpsblistMapper.insert(zwglDpsblist);

            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }


}





